﻿App.Receipt = function() {

    return {

        currentFormValues: {},

        getTopStore: function() {
            var store = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'receipt/getallreceipt'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'object_id',
					fields: [{
						name: 'object_id',
						type: 'string'
					},{
						name: 'objid',
						type: 'string'
					},
					{
						name: 'from_addr',
						allowBlank: false
					},
					{
						name: 'to_addr',
						allowBlank: false
					},
					{
						name: 'from_name',
						allowBlank: false
					},
					{
						name: 'to_name',
						allowBlank: false
					},
					{
						name:'from_tell'
					},
					{
						name: "to_tell"
					},
					{
						name: "op_time",
						type: 'date',
						dateFormat: 'time'
					}]
				})
            });
            return store;
        },

        getForm: function() {
                var getCostTypeStore = new Ext.data.Store({
                    autoLoad:false,
    				baseParams : {parent_id:1},//1表示direct表id为1的记录(次store取出parent为1的所有记录)
    				proxy: new Ext.data.HttpProxy({
    					url : 'menu/costype'
    				}),
                    remoteSort: false,
    				reader: new Ext.data.JsonReader({
    					totalProperty : 'totalCount',
    					root : 'rows',
    					idProperty : 'id',
    					fields: [{
    						name: 'name',
    						type:'string'
    					},{
    						name: 'id',
    						type: 'string'
    					}]
    				})
                });
               
        	var transitStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'transit/items'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'transit_id',
					fields: [{
						name: 'transit_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
            var frm = new Ext.form.FormPanel({
            	url:'rollback/manager',
                labelAlign: 'right',
                frame: true,
                waitMsgTarget:true,
                defaults:{allowBlank :false},
                items: [{
                    layout:'column',
                    items:[{
                        columnWidth:.5,
                        layout: 'form',
                        defaults:{allowBlank :false},
                        defaultType : 'textfield',
                        				items : [
                    	                		{
                    	                			name: 'objid',
                    	                			fieldLabel: '货号'
                    	                		},
                    	                		{
                    	                			name: 'traff_fee',
                    	                			fieldLabel: '运费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		},
                    	                		{
                    	                			name: 'mid_fee',
                    	                			fieldLabel: '中转费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		},
                    	                		{
                    	                			name: 'saf_fee',
                    	                			fieldLabel: '保险费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		},
                    	                		{
                    	                			name: 'inst_fee',
                    	                			fieldLabel: '代收费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		},
                    	                		{
                    	                			name: 'oth_fee',
                    	                			fieldLabel: '其它费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		},
                    	                		{
                    	                			name:'pre_fee',
                    	                			fieldLabel:'垫付费',
                    	                			xtype : 'numberfield',
                    	                            value:0,
                    	                            minValue:0
                    	                		}]
                    },{
                    	defaults:{allowBlank :false},
                        columnWidth:.5,
                        layout: 'form',
                        defaultType : 'textfield',
                        				items : [
                        			        		{
                        			            		xtype: 'combo',
                        			            		fieldLabel:"付款方式",
                        			        			name: 'cost_type',
                        			        			width:130,
                        			        			store :getCostTypeStore,
                        			        			 displayField:'name',
                        			                     valueField :'id',
                        			                     mode: 'remote',
                        			                     triggerAction: 'all',
                        			                     editable: false,
                        			                     emptyText:'选择付款方式....'
                        			        		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_traff_fee',
                        	                			fieldLabel: '实收',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_mid_fee',
                        	                			fieldLabel: '实收',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_saf_fee',
                        	                			fieldLabel: '实收',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_inst_fee',
                        	                			fieldLabel: '实收',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_oth_fee',
                        	                			fieldLabel: '实收',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		},
                        	                		{
                        	                			xtype: 'textfield',
                        	                			name: 't_all_fee',
                        	                			fieldLabel: '汇总',
                        	                			xtype : 'numberfield',
                        	                            value:0,
                        	                            minValue:0
                        	                		}]
                    }]
                },
                {
                	xtype: 'hidden',
                	name:'cost_id'
                	
                },
                {
                	xtype: 'hidden',
                	name:'object_id'
                	
                },{
                	xtype: 'radiogroup', 
                    fieldLabel: '是否结清',
                    items : [
                             {
            					boxLabel: '是',
            					name: 'settle',
            					inputValue:1,
            					checked : true
                             },
                             {
                                 boxLabel: '否',
                                 name: 'settle',
                                 inputValue:0
                             }
                          ] 
                },
                {
                	xtype: 'radiogroup', 
                    fieldLabel: '是否中转',
                    items : [
                             {
            					boxLabel: '是',
            					name: 'transitf',
            					inputValue:1
            					
                             },
                             {
                                 boxLabel: '否',
                                 name: 'transitf',
                                 inputValue:0,
                                 checked : true,
                                 listeners : {
                                     check : function(checkbox, checked) {
                                          if (checked) {
                                        	  this.findParentByType('form').getForm().findField('transit_id').setDisabled(true);//选中报价金额输入框不可用
                                          }else{
                                        	  this.findParentByType('form').getForm().findField('transit_id').setDisabled(false);
                                          }
                                     }
                                }
                             }
                          ] 
                },
                {
                	
                	fieldLabel: '中转公司',
            		xtype:'combo',
            		disabled:true,
            		hiddenName :"transit_id",
            		store: transitStore,
                    displayField:'name',
                    valueField :'transit_id',
                    typeAhead: true,
                    mode: 'remote',
                    forceSelection: true,
                    triggerAction: 'all',
                    emptyText:'请选择中转公司...',
                    selectOnFocus:true,
                    editable: false
                	
            }], 
            buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        var f = this.form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
            }); //FormPanel
            return frm;
        },

        submit: function() {
            if (this.form.form.isValid()) {
				this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                        var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            this.lowerGrid.getStore().reload();
                                this.dlg.hide();
                        }
                        Ext.Msg.alert('信息', info);
                    },
                    failure: function(form, action) {
                        var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
                    }
                });
            }
        },

        getDialog: function() {
            var dlg = new Ext.Window({
                width: 550,
                height: 350,
                title: '',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.form],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.form.form.waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                        this.form.form.setValues(this.currentFormValues);
                        this.form.form.clearInvalid();
                    }
                }
            }); //dlg
            return dlg;
        },
        highLight: function(value) {
        	var key = App.Rollback.store.baseParams.query;
        	var v = value ? value.toString() : '';
        	if (Ext.isString(v) & Ext.isString(key)) {
        		if (key.length > 0) {
        			var re = new RegExp(key, "g");
        			return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
        		} else {
        			return v;
        		}
        	} else {
        		return v;
        	}
        },
        dateformate: function(value) {
        	return value.format("Y-m-d h:i:s");
        },
        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";
            this.gird = this.getTopGrid();
           // this.lowerGrid=this.getLowerGird();
            panel.add(this.gird);
           // panel.add(this.lowerGrid);
            
        },
        
        ret_fwm:function()
        {
        	var retfrm = new Ext.form.FormPanel({
        	url:'rollback/manager',
            labelAlign: 'right',
            frame: true,
            waitMsgTarget:true,
            defaults:{allowBlank :false},
            items: [
            {
            	xtype: 'hidden',
            	name:'cost_id'
            	
            },
            {
            	xtype: 'hidden',
            	name:'object_id'
            	
            },
            {
    			name: 'obj_name',
    			readOnly:true,
    			fieldLabel: '返还货名'
    		},
            {
    			name: 'obj_num',
    			fieldLabel: '返还件数',
    			xtype : 'numberfield'
    		},
            {
    			name: 'traff_fee',
    			fieldLabel: '运费'
    		},
            {
    			name: 'saf_fee',
    			fieldLabel: '保险费'
    		}], 
        buttons: [{
                text: '确定',
                scope: this,
                handler: function() {
                    this.submit();
                }
            },
            {
                text: '重置',
                scope: this,
                handler: function() {
                    var f = this.form.getForm();
                    f.reset();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }]
        }); //FormPanel
        return retfrm;
    },
        getTopGrid: function (){
        	
        	var sm = new Ext.grid.CheckboxSelectionModel();
            var topGrid = new Ext.grid.GridPanel({
                tbar: new Ext.PagingToolbar({
                    pageSize: 23,
                    displayInfo: true,
                    store: this.store,
                    items: ['-', 
                            {
                        text: '发货管理',
                        scope: this,
                        handler: function(){
	                    	if(this.gird.getSelectionModel().hasSelection()){
	                    		var selectdata = this.gird.getSelectionModel().getSelected().data;
		                    	Ext.apply(this.currentFormValues, {
		                    		object_id:selectdata.object_id,
		                    		objid:selectdata.objid,
		                    		saf_fee:selectdata.saf_fee,
		                    		mid_fee:selectdata.mid_fee,
		                    		traff_fee:selectdata.traff_fee,
		                    		inst_fee:selectdata.inst_fee,
		                    		oth_fee:selectdata.oth_fee,
		                    		t_saf_fee:selectdata.saf_fee,
		                    		t_mid_fee:selectdata.mid_fee,
		                    		t_traff_fee:selectdata.traff_fee,
		                    		t_inst_fee:selectdata.inst_fee,
		                    		t_oth_fee:selectdata.oth_fee,
		                    		cost_id:selectdata.cost_id,
		                    		cost_type:selectdata.cost_type
		                    		
		                       });
		                    	this.add("发货管理",this.getForm(), 600, 300);
	                		}else{
	                			Ext.Msg.alert('提示','请选择货号');
	                		}
                    	}
                    },{

                        text: '原货返回',
                        scope: this,
                        handler: function(){
	                    	if(this.gird.getSelectionModel().hasSelection()){
	                    		var selectdata = this.gird.getSelectionModel().getSelected().data;
		                    	Ext.apply(this.currentFormValues, {
		                    		object_id:selectdata.object_id,
		                    		objid:selectdata.objid,
		                    		saf_fee:selectdata.saf_fee,
		                    		traff_fee:selectdata.traff_fee,
		                    		cost_id:selectdata.cost_id,
		                    		obj_num:selectdata.obj_num
		                    		
		                       });
		                    	this.add("原货返回",this.ret_fwm(), 600, 300);
	                		}else{
	                			Ext.Msg.alert('提示','请选择货号');
	                		}
                    	}
                    
                    },
                    '-', new Ext.ux.form.SearchField({
                        store: this.store,
                        width: 110
                    })]
                }),
                anchor:'top 50%',
                store: this.store,
                sm: sm,
                loadMask: true,
                autoHeight:true,
                plugins: [Ext.ux.plugins.Print],//打印按钮
                viewConfig : {autoFill:false,forceFit:true},
                columns: [sm,new Ext.grid.RowNumberer(), {
                    header: "货号",
                    width: 60,
                    sortable: true,
                    dataIndex: 'objid',
                    renderer: this.highLight
                },
                {
                    header: "发货地",
                    width: 100,
                    sortable: true,
                    dataIndex: 'from_addr',
                    renderer: this.highLight
                },
                {
                    header: "收货地",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_addr',
                    renderer: this.highLight
                },
                {
                    header: "发货人",
                    width: 100,
                    sortable: true,
                    dataIndex: 'from_name',
                    renderer: this.highLight
                },
               {
                    header: "收货人",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_name',
                    renderer: this.highLight
                },
                {
                    header: "发货人电话",
                    width: 100,
                    sortable: true,
                    dataIndex: 'from_tell'
                },
                {
                    header: "收货人电话",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_tell'
                },
                {
                    header: "入库时间",
                    width: 150,
                    sortable: true,
                    dataIndex: 'op_time',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                },
               {
                	header: "付款方式",
                    width: 150,
                    sortable: true,
                    dataIndex: 'cost_type'
                },
                {
                	header:"发货方式",
                	width:150,
                	sortable:true,
                	dataIndex:'send_type'
                },
                {
                    header: "运费",
                    width: 150,
                    sortable: true,
                    dataIndex: 'traff_fee'
                },
                {
                    header: "中转费",
                    width: 150,
                    sortable: true,
                    dataIndex: 'mid_fee'
                },
                {
                    header: "保险费",
                    width: 150,
                    sortable: true,
                    dataIndex: 'saf_fee'
                },
                {
                    header: "代收",
                    width: 150,
                    sortable: true,
                    dataIndex: 'inst_fee'
                },
                {
                    header: "其它费",
                    width: 150,
                    sortable: true,
                    dataIndex: 'oth_fee'
                }]
            });
            return topGrid;
        },
        getLowerGird: function(){
        	var lowerStore = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'inhouse/items_o'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'object_id',
					fields: [{
						name: 'object_id',
						type: 'string'
					},
					{
						name: 'objid',
						type: 'string'
					},
					{
						name: 'from_addr',
						allowBlank: false
					},
					{
						name: 'to_addr',
						allowBlank: false
					},
					{
						name: 'from_name',
						allowBlank: false
					},
					{
						name: 'to_name',
						allowBlank: false
					},
					{
						name:'from_tell'
					},
					{
						name:'cost_type'
					},
					{
						name: "to_tell"
					},
					{
						name:"send_type"
					},
					{
						name: "op_time",
						type: 'date',
						dateFormat: 'time'
					}]
				})
            });
        	var sm = new Ext.grid.CheckboxSelectionModel();
            var lowerGird = new Ext.grid.GridPanel({
            	anchor:'-0 50%',
            	width:"100%",
                loadMask: true,
                ref:"lowerGird",
                autoHeight:true,
                plugins: [Ext.ux.plugins.Print],//打印按钮
                viewConfig : {autoFill:false,forceFit:true},
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 25 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: lowerStore,
                    items:[
	               '-', new Ext.ux.form.SearchField({
                    store: lowerStore,
                    width: 110
                })]}),
                store: lowerStore,
                sm: sm,
                columns: [new Ext.grid.RowNumberer(), {
                    header: "货号",
                    width: 100,
                    sortable: true,
                    dataIndex: 'objid',
                    renderer: this.highLight
                },
                {
                    header: "发货地",
                    width: 150,
                    sortable: true,
                    dataIndex: 'from_addr',
                    renderer: this.highLight
                },
                {
                    header: "收货地",
                    width: 150,
                    sortable: true,
                    dataIndex: 'to_addr',
                    renderer: this.highLight
                },
            /*   {
                    header: "发货人",
                    width: 60,
                    sortable: true,
                    dataIndex: 'from_name',
                    renderer: this.highLight
                },
               */ {
                    header: "收货人",
                    width: 60,
                    sortable: true,
                    dataIndex: 'to_name',
                    renderer: this.highLight
                },
            /*    {
                    header: "发货人电话",
                    width: 100,
                    sortable: true,
                    dataIndex: 'from_tell'
                },
              */  {
                    header: "收货人电话",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_tell'
                },{
                    header: "发货方式",
                    width: 100,
                    sortable: true,
                    dataIndex: 'send_type'
                },
                {
                	header: "付款方式",
                    width: 150,
                    sortable: true,
                    dataIndex: 'cost_type'
                },
                {
                    header: "入库时间",
                    width: 150,
                    sortable: true,
                    dataIndex: 'op_time',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                }]
            });
            return lowerGird;
        },
        save:function(ids,logistics_id,wingrid,lowergrid){
        	Ext.Ajax.request({
				method:'post',
				url:"product/savelogobj",
				params:{
					ids:ids,
					logistics_id:logistics_id
				},
				success: function(resp, opts) {
					var result = Ext.util.JSON.decode(resp.responseText);
					var info = result.info;
					if(result.success=='true') {
						Ext.Msg.alert('信息', info);
						wingrid.store.reload();//刷新弹出窗口数据
						lowergrid.store.reload();//刷新lowergrid
					}else {
                        Ext.Msg.alert('信息', info);
                    }
				},
				failure: function(resp,opts) {
					var result = Ext.util.JSON.decode(resp.responseText);
					var info = result.info;
					Ext.Msg.alert('提示', info); 
				}
			});
        },
        add: function(tile,cmp,width,height) {
        	this.dlg.removeAll(true);
        	this.dlg.setTitle(tile);
        	this.dlg.setSize(width,height);
        	this.dlg.add(cmp);
        	this.dlg.show();
        },

        edit: function() {
            if (this.topGrid.getSelectionModel().hasSelection()) {
                this.dlg.setTitle("发货管理");
                var rec = this.topGrid.getSelectionModel().getSelected();
                Ext.apply(this.currentFormValues, {
                    id: rec.data.id,
                    code: rec.data.id,
                    name: rec.data.name,
                    spec: rec.data.spec,
                    object_id: rec.data.object_id,
                    price: rec.data.type,
                    cost_type:rec.data.cost_type
                });
                this.dlg.show();
            } else {
                Ext.Msg.alert('信息', '请选择货号进行发货。');
            }
        },

        del: function(grid,url) {
            if (grid.getSelectionModel().hasSelection()) {
                var recs = grid.getSelectionModel().getSelections();
                var ids = [];
                for(var i = 0; i < recs.length; i++) {
                    ids.push(recs[i].id);
                }
                Ext.Msg.confirm('删除产品', '确定删除吗？',
                function(btn) {
                    if (btn == 'yes') {
						Ext.Ajax.request({
							method:'post',
							url:url,
							params:{
								ids:ids.toString()
							},
							success: function(resp, opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								if(result.success=='true') {
									Ext.Msg.alert('信息', info);
									grid.store.reload();
								}else {
                                    Ext.Msg.alert('信息', info);
                                }
							},
							failure: function(resp,opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								Ext.Msg.alert('提示', info); 
							}
						});
                    }
                });
            } else {
                Ext.Msg.alert('信息', '请选择要删除的产品！');
            }
        },

        render: function(id) {
            if (!this.store) {
                this.store = this.getTopStore();
            }; //if(!this.store)
            if (!this.form) {
                this.form = this.getForm();
            }; //if(!this.form)

            if (!this.dlg) {
                this.dlg = this.getDialog();
            }; //if(!this.dlg)
            this.createGrid(id);
        }

    }; //return
} ();
